/* CustomerController.java
 *
 * Copyright (C) 2013 Universidad de Sevilla
 * 
 * The use of this project is hereby constrained to the conditions of the 
 * TDG Licence, a copy of which you may download from 
 * http://www.tdg-seville.info/License.html
 * 
 */

package controllers.customer;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import services.ActorService;
import services.GalleryService;
import services.PaintingService;
import controllers.AbstractController;
import domain.Gallery;
import domain.Painting;
import forms.PaintingForm;

@Controller
@RequestMapping("/painting/customer")
public class PaintingCustomerController extends AbstractController {

	//Services -----------------------------------------------------------------
	
	@Autowired
	PaintingService paintingService;
	
	@Autowired
	GalleryService galleryService;
	
	@Autowired
	ActorService actorService;
	// Constructors -----------------------------------------------------------

	public PaintingCustomerController() {
		super();
	}
	

	//Create and Edit
	// Creation ---------------------------------------------------------------

		@RequestMapping(value = "/create", method = RequestMethod.GET)
		public ModelAndView create() {
			ModelAndView result;
			

			PaintingForm paintingForm = new PaintingForm();
			result = createEditModelAndView(paintingForm);
		

			return result;
		}

		// Edition ----------------------------------------------------------------

		@RequestMapping(value = "/edit", method = RequestMethod.GET)
		public ModelAndView edit(@RequestParam int paintingId) {
			ModelAndView result;
			PaintingForm paintingForm;
			paintingForm = new PaintingForm(paintingService.findOne(paintingId));		
			result = createEditModelAndView(paintingForm);

			return result;
		}
		
	@RequestMapping(value = "/edit", method = RequestMethod.POST, params = "save")
	public ModelAndView save(@Valid PaintingForm paintingForm, BindingResult bindingResult) {
		ModelAndView result;		


		if (bindingResult.hasErrors()) {
			result = createEditModelAndView(paintingForm);

		} else {
			try {
				
				paintingService.save(paintingService.reconstruct(paintingForm));				
				result = new ModelAndView("redirect:/painting/customer/listCreated.do");
				result.addObject("message", "painting.commit.success");
			} catch (Throwable oops) {
				result = createEditModelAndView(paintingForm, oops.getMessage());
			}
		}

		return result;
	}


	// Ancillary methods ------------------------------------------------------

	protected ModelAndView createEditModelAndView(PaintingForm paintingForm) {
		ModelAndView result;

		result = createEditModelAndView(paintingForm, null);

		return result;
	}

	protected ModelAndView createEditModelAndView(PaintingForm paintingForm, String message) {
		ModelAndView result;


		result = new ModelAndView("painting/edit");
		result.addObject("paintingForm", paintingForm);
		result.addObject("message", message);


		return result;
	}
	
//List
	
	@RequestMapping("/listCreated")
	public ModelAndView listCreated(@RequestParam(required=false) String message) {
		ModelAndView result;
		Collection<Painting> paintings;
		paintings = paintingService.findPaintingsByPrincipal();
		
		
		result = new ModelAndView("painting/list");
		result.addObject("paintings", paintings);
		result.addObject("URI", "/painting/customer/listCreated.do");
		result.addObject("message", message);
		return result;
	}
	
	@RequestMapping("/listWithoutGalleries")
	public ModelAndView listWithoutGalleries() {
		ModelAndView result;
		Collection<Painting> paintings;
		paintings = paintingService.findPaintingsWithoutGalleriesAndByPrincipal();
		
		
		result = new ModelAndView("painting/list");
		result.addObject("paintings", paintings);
		result.addObject("URI", "/painting/customer/listWithoutGalleries.do");
		return result;
	}
	
	
	
	@RequestMapping("/addToGallery")
	public ModelAndView addToGallery(@RequestParam int paintingId, @RequestParam int galleryId) {
		ModelAndView result;
		
		try{
			Painting painting = paintingService.findOne(paintingId);
			Gallery galleryN = galleryService.findOne(galleryId);
			Gallery galleryO = painting.getGallery();
		
			if(galleryO != null){
				galleryO.getPaintings().remove(painting);
				galleryService.save(galleryO);
			}
			
			Collection<Painting> p = galleryN.getPaintings();
			List<Painting> l = new ArrayList<Painting>();
			l.addAll(p);
			l.add(painting);
		
			galleryN.setPaintings(l);
			galleryService.save(galleryN);
			
			painting.setGallery(galleryN);
			paintingService.modify(painting);
			
			result = new ModelAndView("redirect:/painting/customer/listCreated.do");
			result.addObject("message", "painting.addition.success");
			return result;
		}catch(Exception e){
			
			result = new ModelAndView("redirect:/painting/customer/listCreated.do");
			result.addObject("message", "painting.addition.error");
			return result;
			
		}
	}
	
}